/*
 * Miccsnookersim, a snooker simulation with AI
 * Copyright (C) 2009 Enno Ruijters, Frans van den Heuvel, Yannick Thimister,
 * Ali B. Ozmen, Esther Verhoef, Achim Leydecker
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.awt.event.*;

public class MenuHandler implements ActionListener, ItemListener
{
	public static final byte M_NEW_GAME = 1;
	public static final byte M_EXIT = 2;
	public static final byte P_A_MONTECARLO = 3;
	public static final byte P_A_TACTICAL = 4;
	public static final byte P_A_HUMAN = 5;
	public static final byte P_B_MONTECARLO = 6;
	public static final byte P_B_TACTICAL = 7;
	public static final byte P_B_HUMAN = 8;
	public static final byte N_A_ON = 9;
	public static final byte N_A_OFF = 10;
	public static final byte N_B_ON = 11;
	public static final byte N_B_OFF = 12;
	public static final byte N_CONF = 13;
	public static final byte P_A_MONTECARLO2 = 14;
	public static final byte P_B_MONTECARLO2 = 15;
	private byte handlerType;

	private class PlayerSetter implements Runnable
	{
		private Player player;
		private int playerNr;

		public PlayerSetter(Player p, int n)
		{
			player = p;
			playerNr = n;
		}
		public void run()
		{
			synchronized(Game.bigGameLock) {
				if (player.getClass().isInstance(Game.getPlayer(playerNr)))
					return;
				Game.setPlayer(playerNr, player);
			}
		}
	}

	public MenuHandler(byte type)
	{
		handlerType = type;
	}

	public void actionPerformed(ActionEvent ae)
	{
		Thread t;
		PlayerSetter setter;
		switch(handlerType) {
		case M_NEW_GAME:
			Game.newGame();
			break;
		case M_EXIT:
			System.exit(0);
		case P_A_MONTECARLO:
			setter = new PlayerSetter(
			                new MonteCarloAI(Game.getTable(),
				                         Game.getDisplay(),
				                         "Monte Carlo AI (A)"),
					0);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_A_MONTECARLO2:
			setter = new PlayerSetter(
			                new MonteCarloAI2(Game.getTable(),
				                          Game.getDisplay(),
				                          "Monte Carlo AI2 (A)"),
					0);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_A_TACTICAL:
			setter = new PlayerSetter(
			                new TacticalAI(Game.getTable(),
				                       "Tactical AI (A)"), 0);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_A_HUMAN:
			setter = new PlayerSetter(
			                new HumanPlayer(Game.getDisplay(),
				                       "Player A"), 0);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_B_MONTECARLO:
			setter = new PlayerSetter(
			                new MonteCarloAI(Game.getTable(),
				                         Game.getDisplay(),
				                         "Monte Carlo AI (B)"),
					1);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_B_MONTECARLO2:
			setter = new PlayerSetter(
			                new MonteCarloAI2(Game.getTable(),
				                          Game.getDisplay(),
				                          "Monte Carlo AI2 (A)"),
					1);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_B_TACTICAL:
			setter = new PlayerSetter(
			                new TacticalAI(Game.getTable(),
				                       "Tactical AI (B)"), 1);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case P_B_HUMAN:
			setter = new PlayerSetter(
			                new HumanPlayer(Game.getDisplay(),
				                       "Player B"), 1);
			t = new Thread(setter, "Setting player");
			t.start();
			break;
		case N_CONF:
			Game.getSettings().setVisible(true);
			break;
		default:
			System.out.format("Unknown menu option: %d\n", handlerType);
		}
	}

	public void itemStateChanged(ItemEvent e)
	{
		switch (handlerType) {
		case N_A_OFF:
			Game.setPlayerNoise(0, (e.getStateChange() == e.DESELECTED));
			break;
		case N_A_ON:
			Game.setPlayerNoise(0, (e.getStateChange() == e.SELECTED));
			break;
		case N_B_OFF:
			Game.setPlayerNoise(1, (e.getStateChange() == e.DESELECTED));
			break;
		case N_B_ON:
			Game.setPlayerNoise(1, (e.getStateChange() == e.SELECTED));
			break;
		}
	}
}
